import Vue from 'vue';
import Vuex from 'vuex';
import axios from 'axios'
Vue.use(Vuex);

const store = new Vuex.Store({
  state: {
    info: JSON.parse(sessionStorage.getItem("info"))
  },
  mutations: {
    saveinfo(state, userinfo) {
      sessionStorage.setItem('info', JSON.stringify(userinfo))
    },
    updateinfo(state) {
      state.info = JSON.parse(sessionStorage.getItem("info"))
    }

  },
  //

  actions: {
    async login(context, user) {
      let usr = {}
      let instance = axios.create({
        headers: {
          "content-type": "application/json"
        }
      });
      try {
        let info = await instance.post(`http://192.168.10.19:4000/graphql`, {
          query: `{ userLogin(user_code:"${user.code}",user_pass:"${user.pass}"){
                    id,
                    user_sex,
                    user_name,
                    user_code,
                    user_phone,
                    user_office_phone,
                    company,
                    department,
                    user_lastdate,
                    user_nowdate
                  }}`
        });
        if (info.data.data.userLogin) {
          return info.data.data.userLogin
        }
      } catch (error) {
        console.error(error);
      }

    },
    async getJurisdiction(context, userInfo) {
      let res = {}
      let instance = axios.create({
        headers: {
          "content-type": "application/json"
        }
      });
      try {
        let info = await instance.post(`http://192.168.10.19:4000/graphql`, {
          query: `{ getUserJurisdiction(id:${userInfo.id},type:1)
            {id,parent_id,Jurisdiction_name,Jurisdiction_path,Jurisdiction_type},
             roleFind(id:${userInfo.id}){id,role_name},
             deviceListBy_User(user_name:"${userInfo.user_name}",user_code:"${userInfo.user_code}"){id,brand,barcode,
             serial_number,state, type,factory_time,kind_code,kind_name,remarks,model,device_drive,device_baoxiu,device_peizhi}
            }`
        });
        res.jursidiction = info.data.data.getUserJurisdiction
        res.role = info.data.data.roleFind
        res.deviceList = info.data.data.deviceListBy_User
        return res
      } catch (error) {
        console.error(error);
      }
    }


  }
});

export default store;
